<template>
	<view class="main">
		
		<view class="section">
			<view class="title">小区名称：</view>
			<view class="content">
				<view class="input_bg">
					<input class="input" v-model="sys_name" placeholder="请输入小区名称"></input>
				</view>
			</view>
		</view>
		<view class="section">
			<view class="title">原始地址：</view>
			<view class="content">
				<view class="input_bg">
					<input class="input" v-model="company" placeholder="请输入原始地址"></input>
				</view>
			</view>
		</view>
		<view class="section">
			<view class="title">所在地址：</view>
			<view class="content">
				<view class="input_bg">
					<input class="input" v-model="company" placeholder="请输入所在地址"></input>
					<view class="location">
						<image class="loc" src="../../static/images/index/location.png"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="section">
			<view class="title">提出人：</view>
			<view class="content">
				<view class="input_bg">
					<input class="input" v-model="name" placeholder="请输入提出人"></input>
				</view>
			</view>
		</view>
		<view class="section">
			<view class="title">提出时间：</view>
			<view class="content">
				<view class="input_bg">
					<uni-datetime-picker 
						:calendarIcon="false" 
						type="datetime" 
						:clearIcon="false" 
						v-model="propose_time" :border="false" @change="change" />
				</view>
			</view>
		</view>
		<view class="section">
			<view class="title">问题类型：</view>
			<view class="content">
				<view class="input_bg">
					<input class="input" v-model="cate" placeholder="请输入问题类型"></input>
				</view>
			</view>
		</view>
		<view class="section">
			<view class="title">负责厂家：</view>
			<view class="content">
				<view class="input_bg">
					<input class="input" v-model="cj" placeholder="请输入负责厂家"></input>
				</view>
			</view>
		</view>
		<view class="section">
			<view class="title">问题内容：</view>
			<view class="content">
				<view class="text_bg">
					<textarea class="textarea" v-model="content" placeholder="请输入问题内容"></textarea>
				</view>
			</view>
		</view>
		
		<view class="section bottom">
			<view class="title">问题照片</view>
			<view class="content">
				<view class="imgs">
					<view class="item" v-for="(pre, p_index) in pre_imgs" :key="p_index">
						<image class="image" :src="pre"></image>
						<image class="del" @click="delImg(2,p_index)" src="../../static/images/common/del.png"></image>
					</view>
					<view class="item" @click="choosePhoto(2)" v-if="pre_imgs.length < 2">
						<image class="image" src="../../static/images/common/add-pic.png"></image>
					</view>
				</view>
			</view>
		</view><!-- section end-->
		
		<view class="footer">
			<view class="submit-btn" @click="submitEvent()">确认提交</view>
		</view>
		
	</view><!-- main end-->
</template>

<script>
	// import {image_upload} from "@/api/common";
	import {save} from "@/api/question";
	export default {
		data() {
			return {
				sys_name:'',//
				content:'',//客户电话
				company:'',
				name:'',
				cate:'',
				cj:'',
				pre_imgs:[],//安装前图片
				width:0,
				propose_time:''
			}
		},
		onLoad(options) {
			let getWindowInfo = uni.getWindowInfo()
			let width = getWindowInfo.screenWidth;
			this.width = width;
			
		},
		onShareTimeline() {
			return {
				title: '问题采集',
				//分享朋友圈的参数
				query: "isShare=1&id=",
			}
		},
		onShareAppMessage(){
		},
		created() {
			
		},
		onShow:function(){
			let that = this;
			let store_data = uni.getStorageSync('storage_key3');
			if(store_data){
				that.sys_name = store_data.sys_name;
				that.content = store_data.content;
				that.company = store_data.company;
				that.name = store_data.name;
				that.cate = store_data.cate;
				that.cj = store_data.cj;
				that.propose_time = store_data.propose_time;
				if(store_data['pre_imgs'].length > 0){
					that.pre_imgs = store_data['pre_imgs'].split(',');
				}
			}
			console.info(store_data);
			
		},
		onHide:function(){
			this.saveCache();
		},
		onUnload:function(){
			this.saveCache();
		},
		methods: {
			change(e){
				this.propose_time = e;
			},
			saveCache(){
				var data = {
					sys_name:this.sys_name,
					content:this.content,
					company:this.company,
					name:this.name,
					cate:this.cate,
					cj:this.cj,
					propose_time:this.propose_time,
					pre_imgs:this.pre_imgs.join(',')
				}
				console.info(data)
				uni.setStorageSync('storage_key3',data);
			},
			delImg(type,index){
				this.pre_imgs.some((item, i)=>{
	　　　　　　　　　　if(i==index){
	　　　　　　　　　　　　this.pre_imgs.splice(i, 1)
	　　　　　　　　　　　　return true;
	　　　　　　　　　　}
	　　　　　　　　})
			},
			choosePhoto(type) {
				let that = this;
				that.$util.uploadImageOne('upload/image', function(res) {
					that.pre_imgs.push(res.data.url);
				});
			},
			resetData(){
				// this.sys_name = '';
				this.content = '';
				this.company = '';
				this.name = '';
				this.cate = '';
				// this.cj = '';
				this.propose_time = '';
				this.pre_imgs = [];
			},
			submitEvent(){
				if(this.sys_name.length == 0){
					uni.showToast({
						title:'请输入系统名称',
						icon:"none"
					});
					return;
				}
				if(this.company.length == 0){
					uni.showToast({
						title:'请输入提出公司',
						icon:"none"
					});
					return;
				}
				if(this.name.length == 0){
					uni.showToast({
						title:'请输入提出人',
						icon:"none"
					});
					return;
				}
				if(this.propose_time.length == 0){
					uni.showToast({
						title:'请选择提出时间',
						icon:"none"
					});
					return;
				}
				if(this.cate.length == 0){
					uni.showToast({
						title:'请输入问题类型',
						icon:"none"
					});
					return;
				}
				if(this.cj.length == 0){
					uni.showToast({
						title:'请输入负责厂家',
						icon:"none"
					});
					return;
				}
				if(this.content.length == 0){
					uni.showToast({
						title:'请输入问题内容',
						icon:"none"
					});
					return;
				}
				var data = {
					sys_name:this.sys_name,
					content:this.content,
					company:this.company,
					name:this.name,
					cate:this.cate,
					cj:this.cj,
					propose_time:this.propose_time,
					pre_imgs:this.pre_imgs.join(',')
				}
				console.info(data);
				let that = this;
				uni.showModal({
				  title: '提示信息',
				  content: '确定提交？',
				  cancelText: "取消",
				  confirmText: "确定",
				  confirmColor: '#000',
				  cancelColor: '#ff0000',
				  success: function (res) {
					if (res.confirm) {
						uni.showLoading({
							title:"提交中..."
						});
						save(data).then((res)=>{ 
							uni.hideLoading();
							if(res.status == 200){
								that.hasCache = false;
								uni.removeStorageSync('storage_key3')
								that.resetData();
								uni.showToast({
									title:res.msg,
									icon:"none",
									duration:1000
								});
							}else{
								uni.showToast({
									title:res.msg
								});
							}
						}).catch((error)=>{
							uni.hideLoading(); 
							console.log(error);
						});
					} else if (res.cancel) {
					  console.log('用户点击取消');
					}
				  }
				});
				
			}
		}
	}
</script>

<style lang="scss">
	page {
	  width: 100%;
	  height: 100%;
	  background-color: white;
	}
	.main {
		display: flex;
		flex-direction: column;
		// align-items: center;
		padding: 30rpx;
		padding-bottom: 0;  
		padding-bottom: constant(safe-area-inset-bottom);  
		padding-bottom: env(safe-area-inset-bottom); 
		.mb100{
			padding-bottom: 150rpx;
		}
		.section{
			display: flex;
			flex-direction: column;
			margin-top: 30rpx;
			.title{
				font-size: 28rpx;
			}
			.content{
				display: flex;
				flex-direction: column; 
				margin-top: 10rpx;
				.input_bg{
					display: flex;
					flex-direction: row;
					align-items: center;
					height: 90rpx;
					border: 1px solid #CCC;
					padding-left: 10rpx;
					padding-right: 10rpx;
					.input{
						font-size: 28rpx;
						height: 90rpx;
						line-height: 90rpx;
						flex: 1;
						margin-right: 15rpx;
						.place{
							color: gray;
						}
						.text{
							color: #000000;
						}
					}
					.date{
						height: 80rpx;
					}
					.location{
						display: flex;
						flex-direction: row;
						.loc{
							width: 60rpx;
							height:60rpx;
						}
					}
				}
				.text_bg{
					display: flex;
					flex-direction: column;
					height: 160rpx;
					border: 1px solid #CCC;
					padding: 10rpx;
					.textarea{
						font-size: 28rpx;
					}
				}
				.imgs{
					display: flex;
					// padding-left: 10rpx;
					// padding-right: 10rpx;
					flex-direction: row;
					flex-wrap: wrap;
					.item{
						width: 33.3%;
						height:220rpx; 
						.image{ 
							width:85%;
							height:85%;
						}
						.del{
							width:50rpx;
							height:50rpx;
							margin-left: -30rpx;
							margin-top: -20rpx;
							z-index: 99;
							position: absolute;
						}
					}
				}
				.radio{
					margin-top: 15rpx;
					font-size: 28rpx;
				}
				.sign{
					text-align: center;
					height: 300rpx;
					line-height: 300rpx;
					font-size: 28rpx;
					background-color: #F8F8F8;
				}
			}
			.intro{
				font-size: 28rpx;
				color: red;
				margin-top: 15rpx;
			}
			
		}
		.bottom{
			padding-bottom: 150rpx;
		}
	}
	.footer{
		height: 80rpx;
		width: 100%;
		position: fixed;
		right: 0;
		bottom: 0;
		left: 0;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding-bottom: 0;  
		padding-bottom: constant(safe-area-inset-bottom);  
		padding-bottom: env(safe-area-inset-bottom); 
		.submit-btn{
			background-color: $uni-main-color;
			color: white;
			font-size: 28rpx;
			height: 80rpx;
			width: 100%;
			text-align: center;
			line-height: 80rpx;
		}
	}
	/****/
	.popup-cls{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}
	
	.pop-info{
		width:100%;
		background-color: white;
		display: flex;
		flex-direction: column;
		.pupop-btn{
			height: 40px;
			display: flex;
			align-items: center;
			padding: 0 30upx;
			justify-content: space-between;
			font-size: 28rpx;
		}
	}
	.pop-top{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		font-weight: bold;
		color: #000000;
		margin-top: 20rpx;
		text-align: center;
		height: 40rpx;
	}
	
	.center{
		height: 420rpx;
		margin: 30rpx 0rpx 0rpx 0rpx;
		background-color: #F8F8F8;
	}
	
	.bottom{
		display: flex;
		flex-direction: row;
		justify-content: center;
		.btn{
			flex: 1;
			height:80rpx;
			background-color: $uni-main-color;
			// border-radius: 40rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 80rpx;
			// margin-bottom: 30rpx;
			font-size: 28rpx;
		}
		.mr1{
			margin-right: 1rpx;
		}
	}
	
	.picker-view {
		width: 100%;
		height: 340rpx;
		margin-top: 20rpx;
		.item {
			height: 50px;
			align-items: center;
			justify-content: center;
			text-align: center;
			line-height: 50px;
			font-size: 28rpx;
		}
	}
</style>
